﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CsharpTpl.model;
using Lephone.Data;
using CsharpTpl.common;
using Lephone.Data.SqlEntry;

namespace CsharpTpl.window
{
    public partial class FrmRoleTree : Form
    {
        private long _JSID;
        private List<long> jsgnids = new List<long>();
        public FrmRoleTree(long jsid)
        {
            _JSID = jsid;
            List<T_JSGN> jsgns = DbEntry.Context.ExecuteList<T_JSGN>("select * from t_jsgn where jsid=" + _JSID);
            foreach (T_JSGN gn in jsgns)
            {
                jsgnids.Add(gn.GNID);
            }
            InitializeComponent();
            RoleTree.AfterCheck += new TreeViewEventHandler(RoleTree_AfterCheck);
            LoadTree(null, 0);
            btnSave.Click += new EventHandler(btnSave_Click);
            btnClose.Click += new EventHandler(btnClose_Click);
        }

        void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            List<TreeNode> selectedNodes = new List<TreeNode>();
            TreeViewCheck.GetSelectedTreeNode(RoleTree.Nodes, selectedNodes);
            T_JSGN jsgn;
            DbEntry.UsingTransaction(delegate
            {
                DbEntry.Context.ExecuteNonQuery("delete from t_jsgn where jsid="+_JSID);
                foreach (TreeNode node in selectedNodes)
                {
                    jsgn = T_JSGN.New;
                    jsgn.JSID = _JSID;
                    jsgn.GNID = long.Parse(node.Name);
                    jsgn.Save();
                }
            });
            
            MessageBox.Show("保存成功!");
        }

        void RoleTree_AfterCheck(object sender, TreeViewEventArgs e)
        {
            TreeViewCheck.CheckControl(e);
        }

        void LoadTree(TreeNode parent,long fid)
        {
            List<T_GN> menus = T_GN.Find(CK.K["FID"]==fid,new OrderBy("SXH"));
            if (menus.Count > 0)
            {
                TreeNode node;
                foreach (T_GN menu in menus)
                {
                    if (menu.LX == 1)
                    {
                        node = new TreeNode(menu.MC);
                        //node.Tag = menu;
                        node.Name = menu.Id.ToString();
                        if (parent == null)
                        {
                            RoleTree.Nodes.Add(node);
                        }
                        else
                        {
                            parent.Nodes.Add(node);
                        }
                        if (jsgnids.Contains(menu.Id))
                        {
                            node.Checked = true;
                        }
                        LoadTree(node, menu.Id);
                    }
                }
            }
        }
    }
}
